package com.shen.accountbook.module.home.sortmanagefragment;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;

import com.shen.accountbook.bean.temp.FirstSortBean;
import com.shen.accountbook.bean.temp.SecondSortBean;
import com.shen.accountbook.bean.temp.ThirdSortBean;
import com.shen.accountbook.bean.temp.biz.FirstSortBeanBiz;
import com.shen.accountbook.bean.temp.biz.SecondSortBeanBiz;
import com.shen.accountbook.bean.temp.biz.ThirdSortBeanBiz;
import com.shen.accountbook.module.base.BasePresenter;
import com.shen.accountbook.utils.ToastUtils;

import java.util.List;

/**
 * author:  shen
 * date:    2018/6/27
 */
public class SortManagePresenterImpl extends BasePresenter<SortManageView, SortManageModelImpl> {

    public SortManagePresenterImpl(Context context, SortManageView mvpView) {
        super(context, mvpView);
    }

    @Override
    protected SortManageModelImpl createModel() {
        return new SortManageModelImpl();
    }


    protected LinearLayoutManager getLayoutManager(){
        LinearLayoutManager manager = new LinearLayoutManager(mContext);    // 设置布局管理器
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        return manager;
    }

    /**
     * 设置适配器
     *
     * @param bean
     * @param <T>
     * @return
     */
    protected <T> SortManageAdapter setSortAdapter(T bean){
        SortManageAdapter adapter = null;
        SortManageAdapter.ItemOnClickListen listen = null;
        List list = null;
        if(bean == null){
            list = getFirstSortList();
            listen = new SortManageAdapter.ItemOnClickListen() {
                @Override
                public void itemOnClick(Object firstBean) {
                    mvpView.setFirstItemOnClick((FirstSortBean)firstBean);
                }
            };
            adapter = new SortManageAdapter<FirstSortBean>(mContext, list, listen);

        }else if(bean instanceof FirstSortBean){
            list = getSecondSortList((FirstSortBean)bean);
            listen = new SortManageAdapter.ItemOnClickListen() {
                @Override
                public void itemOnClick(Object secondBean) {
                    mvpView.setSecondItemOnClick((SecondSortBean)secondBean);
                }
            };
            adapter = new SortManageAdapter<SecondSortBean>(mContext, list, listen);

        }else if(bean instanceof SecondSortBean){
            list = getThirdSortList((SecondSortBean)bean);
            listen = new SortManageAdapter.ItemOnClickListen() {
                @Override
                public void itemOnClick(Object sortBean) {
                    if(sortBean instanceof SecondSortBean)
                        mvpView.setThirdItemOnClick((ThirdSortBean)sortBean);
                }
            };
            adapter = new SortManageAdapter<ThirdSortBean>(mContext, list, listen);

        }

        return adapter;
    }

    /**
     * 数据库中获取"一级分类"
     * @return
     */
    protected List<FirstSortBean> getFirstSortList(){
        FirstSortBeanBiz biz = new FirstSortBeanBiz();
        return biz.getFirstSortBean();
    }

    /**
     * 数据库中获取"二级分类"
     * @return
     */
    protected List<SecondSortBean> getSecondSortList(FirstSortBean bean){
        SecondSortBeanBiz biz = new SecondSortBeanBiz();
        return biz.getBeanByFirstId(bean.getFirst_sort_id());
    }

    /**
     * 数据库中获取"三级分类"
     * @return
     */
    protected List<ThirdSortBean> getThirdSortList(SecondSortBean bean) {
        ThirdSortBeanBiz biz = new ThirdSortBeanBiz();
        return biz.getBeanBySecondId(bean.getSecond_sort_id());
    }


    /**
     * 添加一个"一级分类"
     * @return
     */
    protected void insertFirstSort(String firstName){
        FirstSortBeanBiz biz = new FirstSortBeanBiz();
        boolean b = biz.insertAuto(firstName);
        showToast(b, firstName);
        mvpView.dataSetChanged(null);
    }

    /**
     * 添加一个"二级分类"
     * @return
     */
    protected void insertSecondSort(FirstSortBean firstSortBean, String secondName){
        SecondSortBeanBiz biz = new SecondSortBeanBiz();
        boolean b = biz.insertAuto(firstSortBean.getFirst_sort_id(), secondName);
        showToast(b, secondName);
        mvpView.dataSetChanged(firstSortBean);
    }

    /**
     * 添加一个"三级分类"
     * @return
     */
    protected void insertThirdSort(SecondSortBean secondSortBean, String thirdName){
        ThirdSortBeanBiz biz = new ThirdSortBeanBiz();
        boolean b = biz.insertAuto(secondSortBean.getFirst_sort_id(), secondSortBean.getSecond_sort_id(), thirdName, "");
        showToast(b, thirdName);
        mvpView.dataSetChanged(secondSortBean);
    }


    private void showToast(boolean b, String name){
        if(b){
            ToastUtils.showToast("添加\"" + name + "\"成功");
        }else {
            ToastUtils.showToast("添加\"" + name + "\"失败");
        }
    }
}
